package threecard.android.card;

import java.lang.reflect.Field;

import threecard.android.R;

import android.util.Log;

public class PlayingCard implements Comparable<PlayingCard> {

	/** Suits */
	protected static final int HEARTS = 4;
	protected static final int DIAMOND = 3;
	protected static final int CLUBS = 2;
	protected static final int SPADES = 1;

	/** Value can be from 1 to 13 : A to K */
	private int value;

	/** Suit can be HEARTS, SPADES, ... */
	private int suit;

	/**
	 * @return The value of the card
	 */
	public int getValue() {
		return value;
	}

	/**
	 * @param value, an integer from 1 - 13, A to K.
	 * @return The value of the card
	 */
	public void setValue(int value) {
		this.value = value;
	}

	/**
	 * @return The character indicating the suit.
	 */
	public int getSuit() {
		return suit;
	}

	/**
	 * 
	 * @param suit
	 *            , 'h', 'd', 'c', 's', which stands for heart, diamonds, ...
	 *            respectively.
	 * @return set suit to be the suit of the card.
	 */
	public void setSuit(int suit) {
		this.suit = suit;
	}

	/**
	 * Constructor.
	 * 
	 * @param value
	 *            Value of the card.
	 * @param suit
	 *            Suit of the card.
	 */
	public PlayingCard(int value, int suit) {
		super();
		this.value = value;
		this.suit = suit;
	}

	/**
	 * 
	 * @return The name of the image representing this card.
	 */
	public String getPngName() {
		char ch = 0;
		switch (suit) {
		case 4:
			ch = 'h';
			break;
		case 3:
			ch = 'd';
			break;
		case 2:
			ch = 'c';
			break;
		case 1:
		default:
			ch = 's';
			break;
		}
		return "" + ch + value;
	}

	public int getImageId() {
		Field f;
		int id = -1;
		try {
			f = R.drawable.class.getDeclaredField(getPngName());
			id = f.getInt(null);
		} catch (Exception e) {
			Log.e("getImageID", "Cannot get image resource: " + e.getMessage());
		}

		return id;
	}
	

	@Override
	public int compareTo(PlayingCard another) {
		if (this.value != another.value) {
			return this.value - another.value;
		} else {
			return this.suit - another.suit;
		}
	}
	
	public Boolean equals(PlayingCard another){
		return (compareTo(another) == 0);
	}

}
